package com.tmxk.api.days01.days01_wmh.set;

import com.tmxk.api.days01.days01_wmh.list.Dog;

import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;

public class SetDemo {
    public static void main(String[] args) {
        //
        //创建了五个狗狗对象
        Dog dog1 = new Dog("小花", 2);
        Dog dog2 = new Dog("小白", 3);
        Dog dog3 = new Dog("小黑", 1);
        Dog dog4 = new Dog("小灰", 5);
        Dog dog5 = new Dog("小青", 6);
        //创建Set集合对象，父类对象引用指向子类实例(多态体现)
        HashSet<Dog> dogSet = new HashSet<>();
        //添加元素
        dogSet.add(dog1);
        dogSet.add(dog2);
        dogSet.add(dog3);
        dogSet.add(dog4);
        dogSet.add(dog5);
        dogSet.add(dog5);
        //获取HashSet集合元素的大小
        System.out.println("dogSet的长度：" + dogSet.size());
        //获取set集合元素
        System.out.println("使用for循环遍历 --》方式一");
        for (Dog dog : dogSet) {
            System.out.print(dog.getName() + "\t" + dog.getAge() + "\t\t");
        }


        // Iterator 可理解成专门为 集合框架遍历元素而产生的
        System.out.println("\n\n使用lambda表达式遍历    方式二");
        dogSet.forEach(dog -> {
            System.out.println();
        });
        System.out.println("\n\n使用迭代器集合元素遍历  方式三");
        //Iterator 可理解为专门为集合框架遍历元素而产生的。
        Iterator<Dog> it = dogSet.iterator();
        while (it.hasNext()) {
            Dog dog = it.next();
            System.out.println(dog.getName() + "\t" + dog.getName());

        }

        //TreeSet
        TreeSet<Dog> dogTreeSet = new TreeSet<>();
        dogSet.add(dog1);
        dogSet.add(dog2);
        dogSet.add(dog3);
        dogSet.add(dog4);
        dogSet.add(dog5);
        dogSet.add(dog5);
        System.out.println("dogTreeSet的集合长度为:" + dogTreeSet.size());
        System.out.println("循环遍历dogTreeSet集合:");
        for (Object obj : dogTreeSet) {
            System.out.println(obj);
        }
    }
}
